package comxin.demo1.jdbcdemo;

import comxin.demo1.Utils.JdbcUtils;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class sqldemo_02 {
    public static void main(String[] args) {
        Map<String,String> map=initUI();
        boolean loginSuccess=Login(map);
        System.out.println(loginSuccess?"登录成功":"登录失败");
    }
    public  static Map<String,String > initUI(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入账号");
        String usernane=scanner.nextLine();
        System.out.println("请输入密码");
        String password=scanner.nextLine();
        Map<String,String> maptemp=new HashMap<>();
        maptemp.put("username",usernane);
        maptemp.put("password",password);
        return maptemp;
    }
    public static boolean Login(Map<String,String> maptemp){
        boolean  loginSuccess=false;
        Connection conn=null;
        PreparedStatement psta=null;
        ResultSet re=null;

        try {
            conn= JdbcUtils.getConnection();
            //String sql1="select * from `idpassword` where `username`= ? and `password`= ? ";
            //String sql = "select * from idpassword where `username`=? and `password`=?";


            String sql="select * from idpassword where `username`=?  and `password`=?";


            psta =conn.prepareStatement(sql);
            psta.setString(1,maptemp.get("username"));
            psta.setString(2,maptemp.get("password"));
            re = psta.executeQuery();
            if (re.next()){
                loginSuccess=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn,psta,re);
        }
        return loginSuccess;
    }
}
